package com.fzb.zrlog.simple.controller;

import com.fzb.zrlog.simple.dao.api.ILogDAO;
import com.fzb.zrlog.simple.dao.impl.Log;
import com.hibegin.http.server.api.HttpRequest;
import com.hibegin.http.server.api.HttpResponse;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

public class QueryLogControl extends BaseControl {

    private ILogDAO logDAO;

    public QueryLogControl(HttpRequest request, HttpResponse response) {
        super(request, response);
        logDAO = new Log();
    }

    public void index() {
        if (getRequest().getUri().startsWith("/post") && getPara(0) != null) {
            //如果有传入 LogId 就进行查询详情
            if (getPara(0).equals("all")) {
                all();
            } else if (getPara(0) != null) {
                detail();
            }
        }
        //定向到主页
        else {
            all();
        }
    }

    /**
     * 通过文章的标题和内容进行内容的检索
     */
    public void search() {
        if (getParaToInt(1) == null) {
            setAttr("data", logDAO.getLogsByTitleOrContent(1, 10, getPara("key")));
            setAttr("yurl", "post/search/" + getPara("key") + "-");
        } else {
            try {
                setAttr("data", logDAO.getLogsByTitleOrContent(getParaToInt(1), 10, URLDecoder.decode(getPara(0), "UTF-8")));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            setAttr("yurl", "post/search/" + getPara(0) + "-");
        }
        response.renderFreeMarker(getTemplatePath() + "/page");
    }

    /**
     * 存档的查找
     */
    public void record() {
        setAttr("data", logDAO.getLogsByData(getParaToInt(1, 1), 10, getPara(0)));
        setAttr("yurl", "post/record/" + getPara(0) + "-");
        response.renderFreeMarker(getTemplatePath() + "/page");
    }

    /**
     * 文章的详细内容
     */
    public void detail() {
        Map<String, Object> log = new HashMap();
        Integer logId = getParaToInt(0);
        if (logId == 0) {
            logId = logDAO.getLogIdByAlias(getPara(0));
        }
        log.putAll(logDAO.getLogByLogId(logId));
        log.put("lastLog", logDAO.getLastLog(logId));
        log.put("nextLog", logDAO.getNextLog(logId));
        setAttr("log", log);
        response.renderFreeMarker(getTemplatePath() + "/detail");
    }

    /**
     * 分类查找
     */
    public void sort() {
        setAttr("data", logDAO.getLogsBySort(getParaToInt(1, 1), 10, getPara(0)));
        setAttr("yurl", "post/sort/" + getPara(0) + "-");
        response.renderFreeMarker(getTemplatePath() + "/page");
    }

    /**
     * 标签方式查找
     */
    public void tag() {
        try {
            setAttr("data", logDAO.getLogsByTag(getParaToInt(1, 1), 10, URLDecoder.decode(getPara(0), "UTF-8")));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        setAttr("yurl", "post/tag/" + getPara(0) + "-");
        response.renderFreeMarker(getTemplatePath() + "/page");
    }

    /**
     * 默认不通过任何字段查找
     */
    public void all() {
        setAttr("data", logDAO.getLogsByPage(getParaToInt(1, 1), 10));
        setAttr("yurl", "post/all-");
        response.renderFreeMarker(getTemplatePath() + "/index");
    }
}
